<?php if (!defined('NOVA_RUN')) die('You can\'t access this file directly!');

/*
-------------------------------------------------------
*
*   NovaBoard 2
*   Copyright © 2011 NovaBoard team
*
*--------------------------------------------------------
*
*   Official site: www.novaboard.net
*   Contact e-mail: support@novaboard.net
*
*   GNU General Public License, version 2:
*   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/

abstract class Admin_Action extends Engine
{
	var $__action = '';
	var $__method = '';

	// container for model objects
	var $UserModel;

	// contained for objects
	var $curUser;

	public function __construct($method)
	{
		$this->__method = $method;
		$this->__action = $this->action;

		parent::__construct();
		$this->__init();
	}

	// gets current action (static only)
	public static function get_action()
	{
		// init Engine
		Engine::initEngine();

		try
		{
			$Router = new Router();
			$action = $Router->getAction();
			$method = $Router->getMethod();
		}
		catch (Exception $e)
		{
			// Set to 404 page
			$action = 'err_404';
			$method = 'view';
		}
		
		self::init_action($action, $method);
	}

	// inits current action (static only)
	private static function init_action($action, $method)
	{
		// require action class
		require ROOT . 'engine/actions/admin/' . $action . '.action.php';

		// make class name
		$action .= '_Action';
		$action[0] = mb_strtoupper($action[0], 'UTF-8');
		
		// run class
		$action = new $action($method);

	}

	// Main init method
	private function __init()
	{
		// load objects...
		require ROOT . 'engine/objects/user.object.php';

		// ... and models
		require ROOT . 'engine/models/user.model.php';
		$this->UserModel = new User_Model();

		// remove old sessions
		$this->UserModel->removeOldSessions(Config::get('session_timeout'));

		// get current user
		$this->curUser = $this->UserModel->check_user('Admin CP');

		// assign tpl vars
		$tpl = tpl::instance();
		$tpl->assign('user', $this->curUser);
		$tpl->assign('lang', lang::instance());

		// check current method
		$this->__action = $this->action;

		if ($this->__method == '')
		{
			$this->__method = $this->default;
		}
		elseif (!method_exists($this, $this->__method))
		{
			Engine::not_found();
		}

		// and run it!
		$this->{$this->__method}();
	}
}